
package com.ssi.core.framework.dao.impl;
import java.sql.Connection;   
import java.sql.SQLException;   
import org.apache.commons.logging.Log;   
import org.apache.commons.logging.LogFactory;   
import com.ssi.core.framework.dao.dialect.Dialect;

import com.ibatis.sqlmap.engine.execution.SqlExecutor;   
import com.ibatis.sqlmap.engine.mapping.statement.RowHandlerCallback;   
import com.ibatis.sqlmap.engine.scope.StatementScope;   

/**
 * 类说明：Ibatis2.3.4 支持物理分页,继承SqlExecutor重新实现�?
 * @创建时间 Jan 8, 2011 4:10:31 PM
 * @创建人： Farley
 * @邮箱：kfy0521@163.com
 */

public class LimitSqlExecutor extends SqlExecutor {
	
	private static final Log logger = LogFactory.getLog(LimitSqlExecutor.class);   
    
    private Dialect dialect;   
    private boolean enableLimit = true;   
    
    public Dialect getDialect() {   
        return dialect;   
    }   
    
    public void setDialect(Dialect dialect) {   
        this.dialect = dialect;   
    }   
    
    public boolean isEnableLimit() {   
        return enableLimit;   
    }   
    
    public void setEnableLimit(boolean enableLimit) {   
        this.enableLimit = enableLimit;   
    }   
    
    @Override  
    public void executeQuery(StatementScope request, Connection conn, String sql,   
            Object[] parameters, int skipResults, int maxResults,   
            RowHandlerCallback callback) throws SQLException {   
        if ((skipResults != NO_SKIPPED_RESULTS || maxResults != NO_MAXIMUM_RESULTS)   
                && supportsLimit()) {   
            sql = dialect.getLimitString(sql, skipResults, maxResults);   
            if(logger.isDebugEnabled()){   
                logger.debug(sql);   
            }   
            skipResults = NO_SKIPPED_RESULTS;   
            maxResults = NO_MAXIMUM_RESULTS;               
        }   
        super.executeQuery(request, conn, sql, parameters, skipResults,   
                maxResults, callback);   
    }   
    
    public boolean supportsLimit() {   
        if (enableLimit && dialect != null) {   
            return dialect.supportsLimit();   
        }   
        return false;   
    }   



}
